package cn.tedu.mvcboottest.controller;

import cn.tedu.mvcboottest.common.response.JsonResult;
import cn.tedu.mvcboottest.common.response.StatuCode;
import cn.tedu.mvcboottest.mapper.UserMapper;
import cn.tedu.mvcboottest.pojo.dto.UserArticleDTO;
import cn.tedu.mvcboottest.pojo.dto.UserLoginDTO;
import cn.tedu.mvcboottest.pojo.entity.MvcArticle;
import cn.tedu.mvcboottest.pojo.vo.ArticleVO;
import cn.tedu.mvcboottest.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/v1/users/")
@Slf4j
@Api(tags = "用户模块")
@Validated
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @ApiOperation("登录功能")
    @PostMapping("login")
    public JsonResult selectUser(@RequestBody UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session){
        String username=userLoginDTO.getUsername();
        String password=userLoginDTO.getPassword();
        UserVO userVO=userMapper.selectUser(username,password);
        log.debug("userVO:"+userVO);
        if(userVO!=null){
            if(userVO.getPassword().equals(userLoginDTO.getPassword())){
                session.setAttribute("user",userVO);
                return JsonResult.ok();
            }
            return new JsonResult(StatuCode.USERNAME_PASSWORD_ERROR);
        }
    return new JsonResult(StatuCode.USERNAME_PASSWORD_ERROR);
    }
    @GetMapping("outlogin")
    @ApiOperation("退出功能")
    public void outlogin(@ApiIgnore HttpSession session){
        session.removeAttribute("user");
    }
    @PostMapping("article")
    @ApiOperation("发表文章功能")
    public JsonResult insertArticle(@RequestBody @Validated UserArticleDTO userArticleDTO,@ApiIgnore HttpSession session){
        UserVO userVO=(UserVO) session.getAttribute("user");
        log.debug("userVO:"+userVO);
        log.debug("userArticleDTO:"+userArticleDTO);
        if(userVO!=null ){
            if(userArticleDTO.getTitle().isEmpty()||userArticleDTO.getContent().isEmpty()){
                return new JsonResult(StatuCode.VALIDATED_ERROR,"标题或者内容不能为空... ... ");
            }
            MvcArticle mvcArticle=new MvcArticle();
            BeanUtils.copyProperties(userArticleDTO,mvcArticle);
            mvcArticle.setUserId(userVO.getId());
            userMapper.insertArticle(mvcArticle);
            return JsonResult.ok();
        }
        return new JsonResult(StatuCode.NOT_LOGIN);
    }
    @GetMapping("selectByArticleId")
    @ApiOperation("查询指定文章功能")
    public JsonResult selectByArticleId(@Range(min = 1,max = 100,message = "文章id必须在1-100之间") @RequestParam Long id){
        ArticleVO articleVO=userMapper.selectByArticleId(id);
        return JsonResult.ok(articleVO);
    }

}
